package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.baidu.location.BDLocation;
import com.chuantou.yuba.R;
import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.TrendsDataTO;
import com.chuantou.yuba.api.to.TrendsTO;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.Constants;

/**
 * 动态通用Fragment
 * 
 * @author dl1001
 * 
 */
public class NearTrendsFragment extends TrendsFragment {
	private BDLocation mLocation;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mLocation = YBApp.get().getLocation();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		mAdapter.showDistance(true);
		startRequest();
		return view;
	}

	@Override
	protected void loadFromCache() {

	}

	@Override
	protected BaseGsonRequest<TrendsDataTO> getRequest(Listener<TrendsDataTO> listener, ErrorListener errorListener) {
		HashMap<String, String> params = new HashMap<String, String>();
		String token = mUser != null ? mUser.getToken() : null;
		long uid = mUser != null ? mUser.getUid() : 0;
		if (!TextUtils.isEmpty(token)) {
			params.put(UriHelper.PARAMS_TOKEN, token);
		}
		if (uid > 0) {
			params.put(UriHelper.PARAMS_UID, String.valueOf(uid));
		}
		params.put(UriHelper.PARAMS_SHOW, "nearby");
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_POINT, mLocation.getLongitude() + "," + mLocation.getLatitude());
		String requestUrl = UriHelper.getTrendsListUrl();
		return new GsonRequest<TrendsDataTO>(Method.POST, requestUrl, listener, errorListener, params, TrendsDataTO.class);
	}

	@Override
	public void onResponse(TrendsDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				List<TrendsTO> trends = response.getTrendsList();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					mAdapter.clear();
				}
				mAdapter.addTrendsList(trends);
			} else {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_trends_content);
	}

}
